<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>vue</title>
  <script src="./js/vue.js"></script>
</head>

<body>
  <div id="app">
    <div>{{total}}</div>
    <hr>
    <input type="number" v-model.number="n1">
    <span>{{msg}}</span>
    <button @click="setComputed">给计算属性赋值</button>
  </div>

  <script>
    const vm = new Vue({
      el: '#app',
      data: {
        n1: 1,
        msg: ''
      },
      // 计算属性  它所依赖的项,可以是一个也可以是N个
      computed: {
        // 简写,它只能获取不能设置操作
        /* total() {
          return this.n1 + this.n2
        } */
        // 标准写法,一般很少用到,只知道有它的存在就可以
        total: {
          get() {
            return this.n1
          },
          set(v) {
            // this.n1 = v
            // console.log(v)
            this.msg = v
          }
        }
      },
      methods: {
        setComputed() {
          if (this.n1 > 3) {
            this.total = '数值过大'
          }
          // this.total = 30
          // console.log(this.total)
        }
      }
    })

  </script>
</body>

</html>